import Vue from 'vue'
import Vuex from 'vuex'
import assemble from '@/common/app/assemble.js'
import config from '@/common/app/config.js'
import cacheKeys from '@/common/app/cacheKeys.js'
import { localStorage } from '@/common/app/storage.js'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // 缓存当前的App配置的主题列表信息
    themeList: localStorage.getObject(cacheKeys.CACHE_THEME_LIST) || [],
    // 缓存当前的App主题信息
    currentTheme: localStorage.getString(cacheKeys.CACHE_CURRENT_THEME) || config.DEFAULT_THEME,
    // 缓存当前的App配置的语种列表信息
    languageList: localStorage.getObject(cacheKeys.CACHE_LANGUAGE_LIST) || [],
    // 缓存当前的App语种信息
    currentLanguage: localStorage.getString(cacheKeys.CACHE_CURRENT_LANGUAGE) || config.DEFAULT_LANGUAGE,
    // 缓存客户端IP地址
    clientAddress: localStorage.getString(cacheKeys.CACHE_CLIENT_ADDRESS) || '',
    // 缓存页面所有路由地址
    allRouters: localStorage.getObject(cacheKeys.CACHE_ROUTER_LIST) || [],
    // 缓存页面所有路由地址
    systemList: localStorage.getObject(cacheKeys.CACHE_SYSTEM_LIST) || {}
  },
  getters: {
    themeList(state) {
      return state.themeList
    },
    currentTheme(state) {
      return state.currentTheme
    },
    languageList(state) {
      return state.languageList
    },
    currentLanguage(state) {
      return state.currentLanguage
    },
    clientAddress(state) {
      return state.clientAddress
    },
    allRouters(state) {
      return state.allRouters
    },
    systemList(state) {
      return state.systemList
    }
  },
  mutations: {
    [cacheKeys.CACHE_THEME_LIST](state, data) {
      state.themeList = data;
      localStorage.setObject(cacheKeys.CACHE_THEME_LIST, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_THEME_LIST)
      }
    },
    [cacheKeys.CACHE_CURRENT_THEME](state, data) {
      state.currentTheme = data;
      localStorage.setString(cacheKeys.CACHE_CURRENT_THEME, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_CURRENT_THEME)
      }
    },
    [cacheKeys.CACHE_LANGUAGE_LIST](state, data) {
      state.languageList = data;
      localStorage.setObject(cacheKeys.CACHE_LANGUAGE_LIST, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_LANGUAGE_LIST)
      }
    },
    [cacheKeys.CACHE_CURRENT_LANGUAGE](state, data) {
      state.currentLanguage = data;
      localStorage.setString(cacheKeys.CACHE_CURRENT_LANGUAGE, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_CURRENT_LANGUAGE)
      }
    },
    [cacheKeys.CACHE_CLIENT_ADDRESS](state, data) {
      state.clientAddress = data;
      localStorage.setString(cacheKeys.CACHE_CLIENT_ADDRESS, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_CLIENT_ADDRESS)
      }
    },
    [cacheKeys.CACHE_ROUTER_LIST](state, data) {
      state.allRouters = data;
      localStorage.setObject(cacheKeys.CACHE_ROUTER_LIST, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_ROUTER_LIST)
      }
    },
    [cacheKeys.CACHE_SYSTEM_LIST](state, data) {
      state.systemList = data;
      localStorage.setObject(cacheKeys.CACHE_SYSTEM_LIST, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_SYSTEM_LIST)
      }
    }
  },
  ...assemble.getAssembleData('store', 'index.js')
})
